package nowcode.链表;


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * TODO
 *
 * @Description
 * @Author Lx
 * @Date 2024/7/29 8:32
 **/
public class 删除链表中重复的元素II {
    static public class ListNode{
        ListNode next=null;
        int val;

        ListNode(int val){
            this.val=val;
        }
    }

    static public ListNode deleteDuplicates (ListNode head) {
        if(head==null){
            return null;
        }

        // write code here
        Set<String> set = new HashSet<>();
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur!=null){
            if(set.contains(cur.val+"")){
                prev.next=cur.next;
                continue;
            }
            set.add(cur.val+"");

            prev=cur;
            cur=cur.next;
        }

        return head;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] s = str.split(",",0);


        ListNode head = new ListNode(new Integer(s[0]));
        ListNode cur = head;
        for (int i = 1; i < s.length; i++) {
            cur.next = new ListNode(new Integer(s[i]));
            cur = cur.next;
        }

        ListNode reshead = deleteDuplicates(head);
        while(reshead!=null){
            System.out.print(reshead.val+"->");
        }
        System.out.println();
        //have some trouble...
    }
}
